<template>
  <!-- 修改个人信息 -->
  <div class="change-container">
    <el-card class="change-card">
      <div class="title">
        <span class="desc">修改个人信息</span>
        <img class="icon" src="@/assets/img/item-img/changeInfo.png" alt="" />
      </div>
      <div class="body">
        <el-form
          ref="changeRef"
          :rules="changeRules"
          :model="personalInfo"
          :file-list="avatarImg"
          label-width="80px"
          hide-required-asterisk
        >
          <el-form-item label="姓名" prop="name">
            <el-input disabled v-model="personalInfo.name"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input disabled v-model="personalInfo.sex"></el-input>
          </el-form-item>
          <el-form-item label="校号">
            <el-input disabled v-model="personalInfo.studentNum"></el-input>
          </el-form-item>
          <el-form-item label="院系" prop="department">
            <el-input v-model="personalInfo.department"></el-input>
          </el-form-item>
          <el-form-item label="班级" prop="class">
            <el-input v-model="personalInfo.class"></el-input>
          </el-form-item>
          <el-form-item label="宿舍号" prop="dormitory">
            <el-input v-model="personalInfo.dormitory"></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="personalInfo.phone"></el-input>
          </el-form-item>
        </el-form>
        <!-- 上传头像 -->
        <!-- <div class="avatar-box">
          <span class="up-title">上传头像</span>
          <el-upload
            class="avatar-uploader"
            :class="{ hide: hideUploadIcon }"
            :action="''"
            list-type="picture-card"
            :auto-upload="false"
            :limit="1"
            :on-preview="handlePictureCardPreviewIcon"
            ref="uploadAvatar"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </div> -->
        <!-- 提交按钮 -->
        <div class="btn-container">
          <el-button type="primary" @click="save">保存</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import commonServe from '../../api/service/common.serve'
export default {
  data() {
    // 验证手机号的正则
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号'))
    }
    return {
      id: window.sessionStorage.getItem('studentId'),
      avatarImg: '', //上传的图片
      personalInfo: {
        name: '',
        sex: '',
        studentNum: '',
        department: '',
        class: '',
        phone: '',
        dormitory: ''
      },

      changeRules: {
        department: [{ required: true, message: '请输入院系', trigger: 'blur' }],
        class: [{ required: true, message: '请输入班级', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ],
        dormitory: [{ required: true, message: '请输入宿舍号', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    // 获取学生信息
    getUserInfo() {
      commonServe.getStudentInfo(this.id).then((res) => {
        console.log(res)
        if (res.code === 200) {
          let obj = res.data.item
          this.personalInfo.name = obj.studentName
          this.personalInfo.sex = obj.sex
          this.personalInfo.studentNum = obj.studentNum
          this.personalInfo.department = obj.department
          this.personalInfo.class = obj.classes
          this.personalInfo.dormitory = obj.dormitoryNum
          this.personalInfo.phone = obj.studentPhone
        }
      })
    },
    // 上传之前获取图片路径
    // handlePictureCardPreviewIcon() {
    //   const isJPG = file.raw.type === 'image/jpeg'
    //   const isPNG = file.raw.type === 'image/png'
    //   const isLt2M = file.raw.size / 1024 / 1024 < 0.5
    //   this.hideUploadIcon = fileList.length >= 1
    //   if (!isPNG && !isJPG) {
    //     this.$message.error('上传图片只能是 JPG/PNG 格式!')
    //     return false
    //   } else if (!isLt2M) {
    //     this.$message.error('上传图片大小不能超过 200kb!')
    //     return false
    //   } else if (isLt2M && (isPNG || isJPG)) {
    //     this.iconformData.img = file.raw //图片的url
    //     this.iconformData.name = file.name //图片的名字
    //   }
    // },
    //提交
    save() {
      this.$refs.changeRef.validate(async (valid) => {
        console.log(valid)
        if (!valid) return
        var params = {
          id: this.id,
          classes: this.personalInfo.class,
          department: this.personalInfo.department,
          dormitoryNum: this.personalInfo.dormitory,
          studentPhone: this.personalInfo.phone
        }
        commonServe
          .changeStudentInfo(params)
          .then((res) => {
            console.log(res)
            if (res.code === 200) {
              this.$message.success('修改信息成功')
            } else {
              this.$message.info('修改信息失败，请刷新重试')
            }
          })
          .catch((err) => {
            this.$message.warning('修改信息失败，请刷新重试')
          })
      })
      // let config = {
      //   timeout: 30000,
      //   Headers: {
      //     'content-Type': 'multipart/form-data'
      //   }
      // }
      // const formData = new formData()
      // for (const key in this.iconformData) {
      //   formData.append(key, this.iconformData[key]) //传参改为formData格式
      // }
    }
  }
}
</script>

<style lang="less" scoped>
.change-container {
  .change-card {
    .title {
      display: flex;
      align-items: center;
      .desc {
        font-size: 30px;
        color: #3a62d7;
        letter-spacing: 10px;
        font-weight: bold;
        text-shadow: 2px 2px 5px #999;
      }
      .icon {
        width: 40px;
        height: 40px;
      }
      .icon:hover {
        transform: translateY(-5px);
        transition: 0.5s;
      }
    }
    .body {
      padding: 0 200px;
      .avatar-box {
        display: flex;
        .up-title {
          padding-right: 50px;
          width: 70px;
          text-align: right;
          font-size: 14px;
          color: #606266;
        }
        .avatar-uploader {
          border: 1px dashed #999;
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;
          .avatar {
            width: 178px;
            height: 178px;
            display: block;
          }
          .el-icon-plus {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
          }
        }
        .avatar-uploader:hover {
          border-color: #409eff;
        }
      }
      .el-form {
        // width: 700px;
        padding-top: 50px;
        .el-form-item {
        }
        .el-input.is-disabled /deep/ .el-input__inner {
          color: #606266;
        }
      }
      .btn-container {
        // padding: 0 20px;
        padding: 200px 20px 0 80px;
        .el-button {
          width: 100%;
          color: #fff;
        }
      }
    }
  }
}
</style>
